/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Builder;

import Clustering.K_Mean.KMean;
import Clustering.K_Mean.MyCluster;
import Clustering.K_Mean.Document;
import DAO.InformationFilter.CategoryDAO;
import DAO.InformationFilter.PageDAO;
import Output.ConsoleOutput;
import Reader.SQLReader;
import Score.Point;
import java.io.Console;
import java.util.List;

/**
 *
 * @author tran tuan tai
 */
public class CategoryBuilder
{
    public void Build()
    {
        CategoryDAO categoryDAO = new CategoryDAO();
        CategoryDAO.DoEmpty();

        PageDAO pageDAO = new PageDAO();
        SQLReader input = new SQLReader();
        input.InitAllPage();

        KMean kMean = new KMean();
        kMean.SetNumClass(97);
        kMean.SetOutput(new ConsoleOutput());

        List<MyCluster> lstCluster = kMean.DoCluster(input, 10);
        for (MyCluster cluster : lstCluster)
        {
            StringBuilder bow = new StringBuilder();
            List<Point> pBow = cluster.GetBOWCentroid();
            for (Point p : pBow)
            {
                bow.append(p.id).append('\t').append(p.relatedness).append('\t');
            }

            StringBuilder boc = new StringBuilder();
            List<Point> pBoc = cluster.GetBOCCentroid();
            for (Point p : pBoc)
            {
                boc.append(p.id).append('\t').append(p.relatedness).append('\t');
            }
            
            int id = -1;
            if (boc.length() != 0 || bow.length() != 0)
            {
                id = categoryDAO.Insert(bow.toString(), boc.toString());
            }
            

            List<Document> lstDoc =  cluster.GetDoc();
            for (Document doc : lstDoc)
            {
                pageDAO.UpdateCategory(doc.GetName(), id);
            }

        }

        kMean.Output();
    }

    public static void main(String[] argvs)
    {
        CategoryBuilder builder = new CategoryBuilder();
        builder.Build();
    }
}
